/******************************************************************************
 * Copyright (C) ShenZhen Powerdata Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳市博安达信息技术股份有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 *****************************************************************************/

package com.szboanda.business.workflow.service.impl;


import com.szboanda.beijing.pure.BaseBusinessService;
import com.szboanda.business.workflow.dao.WorkflowDAO;
import com.szboanda.business.workflow.exception.WorkflowException;
import com.szboanda.business.workflow.service.IWorkflowService;
import com.szboanda.platform.common.utils.LoggerUtil;
import com.szboanda.platform.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @title: 流程处理意见_业务处理service实现类
 * @fileName: WorkflowServiceImpl.java
 * @description:
 * @copyright: PowerData Software Co.,Ltd. Rights Reserved.
 * @company: 深圳市博安达信息技术股份有限公司
 * @author: 原晋川
 * @date: 2019-08-23 15:37
 * @version: V1.0
 */
@Service("WorkflowService")
public class WorkflowServiceImpl extends BaseBusinessService implements IWorkflowService {

    /**
     * 注入业务处理DAO
     */
    @Resource
    private WorkflowDAO workflowDAO;

    /**
     * 查询【流程处理意见】业务集合信息
     *
     * @param modelInfo 业务Map集合
     * @return 返回业务集合
     * @throws WorkflowException
     */
    @Override
    public Map<String, Object> queryWorkflows(Map<String, Object> modelInfo) throws WorkflowException {
        try {
            List<Map<String, Object>> results = workflowDAO.queryWorkflows(modelInfo);

            Map<String, Object> yjResult = new HashMap<>();
            List<Map<String, Object>> yjList = null;

            //放入队列
            Queue<Map<String, Object>> resultQueue = new LinkedList<>(results);

            //遍历队列，根据意见类型进行分组放入Map中
            while (resultQueue.size() > 0) {
                Map<String, Object> yjMap = resultQueue.poll();
                String yjlxdm = yjMap.get("YJLXDM") == null ? "" : yjMap.get("YJLXDM").toString();
                if (StringUtils.isNotEmpty(yjlxdm)) {
                    // 有意见类型代码的处理逻辑
                    if ((yjList = (List<Map<String, Object>>) yjResult.get(yjlxdm)) == null) {
                        yjList = new ArrayList<>();
                        yjResult.put(yjlxdm, yjList);
                    }
                    yjList.add(yjMap);
                }
            }
            return yjResult;

        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "查询流程处理意见列表异常", e);
            throw new WorkflowException("查询流程处理意见列表异常", e);
        }
    }


    /**
     * 根据流程号查询当前的流程节点
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> queryCurrentStepByInstanceid(String params) {
        return workflowDAO.queryCurrentStepByInstanceid(params);
    }

    /**
     * 根据流程号查询该流程开启节点的节点ID
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> queryFirstStepByInstanceid(String params) {
        return workflowDAO.queryFirstStepByInstanceid(params);
    }

    /**
     * 查询指定流程的当前步骤序号及流程对应的业务ID和业务表名
     *
     * @param params {
     *               LCBH,LCDYBH
     *               }
     * @return
     * @throws WorkflowException
     */
    @Override
    public Map<String, Object> queryCurrentStepAndBusinessTableByInstanceId(Map<String, Object> params) throws WorkflowException {
        return workflowDAO.queryCurrentStepAndBusinessTableByInstanceId(params);
    }

    /**
     * 删除指定表的指定数据
     *
     * @param params {
     *               TABLE,YWXTBH
     *               }
     * @return
     * @throws WorkflowException
     */
    @Override
    public int deleteBusinessDataByTableAndInstanceId(Map<String, Object> params) throws WorkflowException {
        return workflowDAO.deleteBusinessDataByTableAndInstanceId(params);
    }

    /**
     * 查询指定用户是否关注指定流程
     *
     * @param userId     用户ID
     * @param workflowId 工作流程实例ID
     * @return 是否关注，true：已关注，false：未关注
     */
    @Override
    public boolean isAttention(String userId, String workflowId) {
        return workflowDAO.queryIsAttention(userId, workflowId);
    }

    /**
     * 设置指定用户关注指定流程
     *
     * @param userId     用户ID
     * @param workflowId 工作流程实例ID
     * @return 执行结果：true：成功， false：失败
     */
    @Override
    public boolean setAttention(String userId, String workflowId) {

        return workflowDAO.setWorkflowAttention(userId, workflowId);
    }

    /**
     * 取消指定用户对指定流程的关注
     *
     * @param userId     用户ID
     * @param workflowId 工作流程实例ID
     * @return 执行结果：true：成功， false：失败
     */
    @Override
    public boolean cancelAttention(String userId, String workflowId) {

        return workflowDAO.cancelWorkflowAttention(userId, workflowId);
    }

    /**
     * 查询业务流程类型集合
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> queryWorkflowTypeMap() {
        return workflowDAO.queryWorkflowType();
    }

    /**
     * 查询业务流程分阅人员
     *
     * @param params
     * @return
     */
    @Override
    public String queryCopyReaders(Map<String, Object> params) {
        List<String> result = workflowDAO.queryCopyReaders(params);
        return result != null && result.size() > 0 ? result.get(0) : "";
    }

}
